﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 谁与争锋可视化设计
{
    public class Statistics
    {
        const int DO_PROBLEMS_INTERVAL_MINUTES = 10;

        delegate int del(date_pro_map dp1, date_pro_map dp2);

        DateTime firstSubmiltTime;
        public DateTime FirstSubmiltTime
        {
            get { return firstSubmiltTime; }
            set { firstSubmiltTime = value; }
        }

        //由年份,月份到日期的一个树状图
        List<int[][]> YMD_Tree;
        private List<int[][]> YMDTree
        {
            get { return YMD_Tree; }
            set { YMD_Tree = value; }
        }

        int[][] YM_Array;
        public int[][] YMArray
        {
            get { return YM_Array; }
            set { YM_Array = value; }
        }


        public class date_pro_map : IComparable
        {
            public DateTime submitTime;
            public int proID;

            //实现按日期进行比较排序的接口
            public int CompareTo(object obj)
            {
                int res = 0;
                date_pro_map dpRight = (date_pro_map)obj;

                try
                {
                    if ((this.submitTime - dpRight.submitTime).TotalSeconds > 0)
                    {
                        res = 1;
                    }
                    else
                    {
                        res = -1;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("比较异常", ex.InnerException);
                }
                return res;
            }
        }

        //根据日期排序的题目表
        List<date_pro_map> dateTable;

        public void AnalysisDailyData(User user)
        {
            List<date_pro_map> dateTable = new List<date_pro_map>();
            User.question[] questionBank = user.QuestionBank;

            foreach (User.question q in questionBank)
            {
                int i = 0;
                do
                {
                    date_pro_map datePro = new date_pro_map();
                    datePro.proID = q.proID;
                    datePro.submitTime = q.submitTime[i];
                    i++;
                    dateTable.Add(datePro);

                } while (i != q.submitTime.Count);
            }

            //按照日期对所有提交成功的题目进行排序
            dateTable.Sort();

            YMD_Tree = new List<int[][]>();
            firstSubmiltTime = dateTable[0].submitTime;

            for (int i = 0; i < DateTime.Now.Year - firstSubmiltTime.Year + 1; i++)
            {
                int[][] year = new int[12][];

                for (int j = 0; j < 12; j++)
                {
                    int[] month = new int[DateTime.DaysInMonth(firstSubmiltTime.Year + i, j + 1)];
                    year[j] = month;
                }

                YMD_Tree.Add(year);
                
            }

            int previousProID = 0;
            DateTime previousTime = DateTime.Parse("1900-01-01 00:00:00");

            foreach(date_pro_map dateItem in dateTable)
            {
                TimeSpan t = dateItem.submitTime - previousTime;

                //与上次做题时间间隔相比大于十分钟才算做一道题, 否则判定为刷题,不计入做题总数
                if ( (previousProID != dateItem.proID) ||
                     (previousProID == dateItem.proID) && (t.Minutes > DO_PROBLEMS_INTERVAL_MINUTES))
                {
                    int year = dateItem.submitTime.Year;
                    int month = dateItem.submitTime.Month;
                    int day = dateItem.submitTime.Day;

                    //该日期做过一道题，在相应的树状图叶子节点处加1
                    YMD_Tree[year - firstSubmiltTime.Year][month - 1][day - 1]++;
                }

                previousTime = dateItem.submitTime;
                previousProID = dateItem.proID;
            }
        }

        public void AnalysisMonthlyData()
        {
            int totalYear = DateTime.Now.Year - firstSubmiltTime.Year + 1;
            YM_Array = new int[totalYear][];

            for (int i = 0; i < totalYear; i++)
            {
                int[] month = new int[12];
                YM_Array[i] = month;

                for (int j = 0; j < 12; j++)
                {
                    for (int k = 0; k < YMD_Tree[i][j].Length; k++)
                    {
                        YM_Array[i][j] += YMD_Tree[i][j][k];
                    }

                }//end of for
            }//end of for

        }
    }
}
